<?php

/**
 * Load models
 *
 * Load models in bundles
 *
 * PHP Version: 1.2.3
 *
 * @author Johari <joharijumali@gmail.com>
 */

namespace Admin\Models\Monitor;

use \Laravel\Database\Eloquent\Model as Eloquent;

/**
 * Data Load Model Class
 *
 * Create model class for lookup data
 *
 * @author Johari <joharijumali@gmail.com>
 */

class Load extends Eloquent
{

    public static $timestamps = true;
    public static $table = 'server_load';
    public static $key = 'load_id';

	/**
     * Function cron that pass parameter
     *
     * @param integer $id   the integer to quote
     * @param varchar $data the varchar to quote
     * @param varchar $date the varchar to quote
	 *
	 * @return parameter.
     */
    public static function cron($id, $data, $date)
	{
        $data = json_decode($data);

        $exist = Load::where('server_id', '=', $id)->first(array('load_id'));

        $load = (empty($exist))? new Load : Load::find($exist->load_id);

        $load->server_id = $id;
        $load->load_one = $data->one;
        $load->load_five = $data->five;
        $load->load_fifteen = $data->fifteen;
        $load->load_uptime = $data->uptime;
        $load->load_user = $data->user;
        $load->load_date = $date;
        $load->save();

        Load::_stor($id, $data, $date, 'stor_load');
    }
	
	/**
     * Function stor that pass parameter
     *
     * @param integer $id    the integer to quote
     * @param varchar $data  the varchar to quote
     * @param varchar $date  the varchar to quote
     * @param varchar $table the varchar to quote
	 *
	 * @return parameter.
     */
    protected static function _stor($id, $data, $date, $table)
	{
        static::$table = $table;

        $load = new Load;

        $load->server_id = $id;
        $load->load_one = $data->one;
        $load->load_five = $data->five;
        $load->load_fifteen = $data->fifteen;
        $load->load_uptime = $data->uptime;
        $load->load_user = $data->user;
        $load->load_date = $date;
        $load->save();
    }

    /**
     * Function grabSingleLoad that retrive parameter
     *
     * @param integer $server_id   tserver id
     *
     * @return parameter.
     */
    public static function grabSingleLoad($server_id)
    {
        
        $load = Load::where('server_id', '=', $server_id)->first()->to_array();

        return $load;
    }


    /**
     * Function grabSingleLoad that retrive parameter
     *
     * @param integer $server_id   tserver id
     *
     * @return parameter.
     */
    public static function grabAllLoad()
    {
        
        $load = Load::all();

        foreach ($load as $key => $value) {
            $load[$key] = $value->to_array();
        }

        return $load;
    }


}

?>